// 企业还款
define(function(require,exports,module){
	
	require('common');
    //检测类
    var Auth = require('qd-auth-1.0');
    //require('qd-common');
	var tpl = require('../../tpl/member_borrow/borrow_repayment.tpl');//载入tpl模板
	var curId = $("#repayDetailDiv");
    var pagerid = "kkpagerRepayDetail";
    var isSetTradePassowd = false; // 是否设定过交易密码

	//初始化显示表格、搜索
    //还款详情
	if(window.location.search.substr(1)){
		if(window.location.search.substr(1).split("=")[0] == "status")
		{
			$("#status a:eq(1)").addClass("current").siblings().removeClass("current");
		}
		require.async(['./search','./showTable'],function(search,showTable){
			search.search('/member/borrow/repaymentList.html?',tpl,curId,pagerid);
			showTable.ajaxUrl('/member/borrow/repaymentList.html?'+window.location.search.substr(1),tpl,curId,pagerid);
			$(".borrow_detail").click(function(){
				showTable.ajaxUrl('/member/borrow/repaymentList.html?'+window.location.search.substr(1),tpl,curId,pagerid);
			});
		});
	}
	else
	{
		require.async(['./search','./showTable'],function(search,showTable){
			search.search('/member/borrow/repaymentList.html?',tpl,curId,pagerid);
			showTable.ajaxUrl('/member/borrow/repaymentList.html?status=99&time=-1',tpl,curId,pagerid);
			$(".borrow_detail").click(function(){
				showTable.ajaxUrl('/member/borrow/repaymentList.html?status=99&time=-1',tpl,curId,pagerid);
			});
		});
	}

    $(".borrowRepay").tabChange({
		isClick:true,
		isHover:false,
		childLi:".title_list li",//tab选项卡
		childContent:".list_div",//tab内容
		hoverClassName:"hover",//选中当前选项卡的样式
		callBack:false	
	});
	// 还款
	$(".repayment").live("click",function(){
		var _this = $(this);
		var validCodeHtml = '<li class="validCodeLi"><strong>验证码：</strong><input name="validCode" type="text" class = "validCode" maxlength="4" style=" width: 108px; *margin-left:3px;" /><img src="/validimg.html" alt="点击刷新" class="valicode_img" onClick="this.src=\'/validimg.html?t=\'+Math.random();" align="absmiddle" style="cursor:pointer;margin-left:10px; height:30px;" /><span class="msg_tip validCodeMsg"></span></li>'		 
		var reMoney = parseFloat(_this.attr("data-tatal")).toFixed(2);
		var lateInterest = parseFloat(_this.attr("data-late")).toFixed(2);
		var repayHtml = '<div class="tipsWrap w384"><form autocomplete="off"><ul><li><strong>应还金额：</strong><span>'+reMoney+'</span>元</li><li><strong>交易密码：</strong><input type="password" name="payPwd" class="payPwd" onpaste="return false" oncontextmenu="return false" oncopy="return false" oncut="return false" autocomplete="off" value=""/></li>'+validCodeHtml+'</ul></form><div class="tipsBtnBar"><a href="javascript:;" class="okBtn">确认还款</a></div></div>'
		if (lateInterest > 0) {
			repayHtml = '<div class="tipsWrap w384"><form autocomplete="off"><ul><li><strong>应还金额：</strong><span>'+reMoney+'</span>元</li><li><strong>逾期利息：</strong><span>'+lateInterest+'</span>元</li><li><strong>交易密码：</strong><input type="password" name="payPwd" class="payPwd" onpaste="return false" oncontextmenu="return false" oncopy="return false" oncut="return false" autocomplete="off" value=""/></li>'+validCodeHtml+'</ul></form><div class="tipsBtnBar"><a href="javascript:;" class="okBtn">确认还款</a></div></div>'
		}

        // 未设定交易密码
        if(isSetTradePassowd == false)
        {
            window.layer.open({
                type: 1,
                closeBtn: 0,
                title: "出错",
                area: ['450px', '190px'],
                border: [1, 1, '#cecfd0'],
                content: '<div class="tipsWrap w450"><div class="tipsTxt"><i class="iconfont tipIco">&#xe63c;</i><span>请先设置交易密码</span></div><div class="tipsBtnBar"><a href="/member/security/setting.html" class="okBtn">马上去设置</a></div></div>'
            });
            return;
        }

        // 显示还款界面
        window.layer.open({
            type :1,
            title : "<span class='tipsTitle'><i class='iconfont'>&#xe642;</i><b>每笔资金受特别保护，安全可靠，请放心操作</b></span>",
            closeBtn :1,
            area: ['420px', '320px'],
            border: [1, 1, '#cecfd0'],
            content: repayHtml
        });

        $(".okBtn").unbind("click").click(function(){
            var postData = {};
            postData._page_token =  $("#_page_token").html() || "";
            postData.payPwd = $(".payPwd").val() || "";
            postData.validCode = $(".validCode").val() || "";

            if(postData.payPwd == "") {
                $.showErrDialogWithClose("支付密码不能为空",function(me){
                    layer.close(me);
                });
            } else if(postData.validCode == "") {
                $.showErrDialogWithClose("验证码不能为空",function(me){
                    layer.close(me);
                });
            } else {
                // 还款
                $.ajax({
                    type:"post",
                    url:'/member/borrow/repay.html?repaymentId=' + _this.attr("data-val") + '&randomTime=' + (new Date()).getTime(),
                    dataType:"json",
                    data:postData,
                    success:function(data){
                        if(data.result){
                            layer.closeAll();
                            window.layer.open({
                                type: 1,
                                closeBtn: 1,
                                title: "&nbsp;",
                                area: ['384px', '186px'],
                                border: [1, 1, '#cecfd0'],
                                time:3000,
                                content: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont okIco">&#xe63d;</i><span>还款成功</span></div><div class="tipsMsg"><i class="tipsTime">3</i>秒后窗口自动关闭</div></div>',
                                cancel: function(){
                                    window.location.href="/member_borrow/borrow/repayment.html";
                                },
                                end: function(){
                                    window.location.href="/member_borrow/borrow/repayment.html";
                                },
                                success:function(){
                                    var time = 3;
                                    function tipsTime(){
                                        time--;
                                        $(".tipsTime").html(time);
                                        if(time == 0)
                                        {
                                            clearInterval(timeCount)
                                        }
                                    }
                                    var timeCount = setInterval(function(){tipsTime()},1000);
                                }
                            });
                        }
                        else
                        {
                            if(data.token) {
                                $("#_page_token").html(data.token);
                            }
                            var url = "javascript:;";
                            if(data.url != null && data.url != ""){
                                url = data.url;
                            }
                            var closeLayer = window.layer.open({
                                type: 1,
                                closeBtn: 1,
                                title: "&nbsp;",
                                area: ['384px', '186px'],
                                border: [1, 1, '#cecfd0'],
                                content: '<div class="tipsWrap w384"><div class="tipsTxt"><i class="iconfont errIco">&#xe63e;</i><span>'+data.msg+'</span></div><div class="tipsBtnBar"><a href="'+url+'" class="okBtn failBtn">确定</a></div></div>',
                                cancel: function(index){
                                    window.location.reload();
                                    layer.close(closeLayer);
                                }
                            });
                            $(".failBtn").click(function(){
                                window.location.reload();
                                layer.close(closeLayer);
                            });
                        }
                    }
                });
            }
        });
	})

   var checkPayPassword = function(){
        // 交易密码是否设置
        $.ajax({
            type:"post",
            url:'/member/borrow/checkPayPassword.html?randomTime=' + (new Date()).getTime(),
            dataType:"json",
            success:function(data){
                if(data.result){
                    isSetTradePassowd = true;
                }
            }
        });
    }
    checkPayPassword();
});